package com.kedou.sdk.ui.activity;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


import com.kedou.sdk.ui.view.DialogFactory;
import com.kedou.sdk.utils.ToolsUtils;
import com.kedou.sdk.utils.WL;

public class ProxyWebActivity extends AppBaseActivity {
	private static final String TAG = ProxyWebActivity.class.getSimpleName();
	private WebView webView;
	private static final String QQ_CUSTOMER_SERVICE = "http://wpd.b.qq.com/page/webchat.html";
	private static final String WECHAT_PUBLIC_NUMBER = "http://www.51wansy.sdkv3.com/";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(getLayoutIdentifier("activity_web"));
		webView =findViewById(getViewIdentifier("webwiew"));

		WebSettings webSettings = webView.getSettings();
		webSettings.setUseWideViewPort(true);
		webSettings.setSupportZoom(true);
		webSettings.setAppCacheEnabled(false);
		webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
		webSettings.setJavaScriptEnabled(true);
		webSettings.setDomStorageEnabled(true);
		webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
		webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
		webView.setWebChromeClient(new ExtensionBarWebChromeClient());
		webView.setWebViewClient(new ExtensionBarWebViewClient());
		webView.loadUrl(getIntent().getStringExtra("url"));
	}
	public static String getValueByName(String url, String name) {
		String result = "";
		int index = url.indexOf("?");
		String temp = url.substring(index + 1);
		String[] keyValue = temp.split("&");
		for (String str : keyValue) {
			if (str.contains(name)) {
				result = str.replace(name + "=", "");
				break;
			}
		}
		return result;
	}
	private class ExtensionBarWebViewClient extends WebViewClient {

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			Log.e("aaa", "shouldOverrideUrlLoading ---->url: " + url);
			if (url.startsWith(QQ_CUSTOMER_SERVICE)) {
				try {
					String qq = getValueByName(url, "nameAccount");

					WL.d(TAG, "shouldOverrideUrlLoading ---->qq: " + qq);

					String localUrl = "mqqwpa://im/chat?chat_type=wpa&uin=" + qq;
					startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(localUrl)));
				} catch (ActivityNotFoundException e) {
					startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
				}
			} else if (url.startsWith(WECHAT_PUBLIC_NUMBER)) {
				if (ToolsUtils.isInstallWeixin(getApplicationContext())) {
					String originalAppId = getValueByName(url, "id");
					String appId = getValueByName(url, "appId");

					ToolsUtils.startWeChatPublicNumber(getApplicationContext(), appId, originalAppId);
				} else {
					String message = getString(getStringIdentifier("hnssn_hint_wechat_uninstall"));
					DialogFactory.createConfirmDialog(ProxyWebActivity.this, message).show();
				}
			}else if (url.startsWith("weixin://")){
				startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
				finish();
			}
			else {
				if (webView != null) {
					webView.loadUrl(url);
				}


			}

			return true;

		}

		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);

		}
	}

	private class ExtensionBarWebChromeClient extends WebChromeClient {




		@Override
		public void onReceivedTitle(WebView view, String title) {
			super.onReceivedTitle(view, title);

		}

		@Override
		public void onProgressChanged(WebView view, int newProgress) {

			super.onProgressChanged(view, newProgress);
		}
	}

}
